package edu.cth.speedapp.student;


import java.util.Scanner;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import edu.cth.speedapp.DeviceUuidFactory;
import edu.cth.speedapp.R;
import edu.cth.speedapp.ScannerActivity;
import edu.cth.speedapp.asynctasks.HTTPAsyncTask;
import edu.cth.speedapp.asynctasks.ITaskCallback;

public class StartActivity extends ScannerActivity implements ITaskCallback{
	
	//Url for http req's. To my super server.
	//public static String baseUrl = "";
	private String roomCode = "";
	
	//View handles
	private View rescanButton;
	private View connectionProgress;
	private TextView qrStatusText;
	private Button manualLoginButton;
	private TextView nfcInstructionText;
    
	
	//Used to suppress rescan on resume when finding an rfid
    private boolean tagRead = false;

    
    
    	
     
	    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		
		//Disable nfc text if nfc is not supported
		nfcInstructionText = (TextView)findViewById(R.id.nfcInstructionText);
		if(!isNFCPossibleOnDevice()){
			nfcInstructionText.setVisibility(View.GONE);
		}

	
		rescanButton = this.findViewById(R.id.rescanButton);
		rescanButton.setVisibility(View.INVISIBLE);
		
		connectionProgress = this.findViewById(R.id.connectionProgress);
		
		connectionProgress.setVisibility(View.INVISIBLE);
		
		qrStatusText = (TextView)findViewById(R.id.qrStatusText);
		
		manualLoginButton = (Button)findViewById(R.id.manualLoginButton);
		manualLoginButton.setVisibility(View.VISIBLE);      
      
        
        
	}
	@Override
	public void tagRead(Tag tag) {
		Log.d("lol", "mytag: lock");
		tagRead = true;
		super.tagRead(tag);
		Log.d("lol", "mytag: unlock");
		
	}

	@Override
	protected void foundCode(String data){
		Log.d("lol", "mytag: foundcode");
		super.foundCode(data);
		roomCode = data;
		qrStatusText.setText("Connecting...");
		if(isNFCPossibleOnDevice()){
			nfcInstructionText.setVisibility(View.INVISIBLE);
		}
		manualLoginButton.setVisibility(View.INVISIBLE);
		connectionProgress.setVisibility(View.VISIBLE);

		
		HTTPAsyncTask joinRoom = new HTTPAsyncTask(this);
		
		 DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(this);
	     String deviceUuid = deviceUuidFactory.getDeviceUuid().toString();      
	       
		
	     joinRoom.execute(getString(R.string.baseURL) + "/joinRoomWithCode.php?code=" + data + "&studentID=" + deviceUuid);
	    
	}
	public void httpTaskCallback(String results) {

		
		if(results == null){
			//Toast.makeText(this, "No http response. :(", Toast.LENGTH_SHORT).show();
			noRoomFound("No response from server");
		}
		
		else if(results.contains("Joined")){
			Toast.makeText(this, "Room joined!", Toast.LENGTH_SHORT).show();
           
			Intent intent = new Intent(this, StudentActivity.class);
           	
    	    
    	    Scanner scan = new Scanner(results);
    	    scan.nextLine();
    	    String roomName = scan.nextLine();
    	    String roomMode = scan.nextLine();
    	    intent.putExtra("roomName", roomName);
    	    intent.putExtra("mode", roomMode);
			startActivity(intent);
			
		}/* else if(results.contains("The student is")){
			Toast.makeText(this, "You are already in this room, re-entering!", Toast.LENGTH_SHORT).show();
           	Intent intent = new Intent(this, StudentActivity.class);
    	    
           	intent.putExtra("roomName", roomName);
    	    intent.putExtra("mode", ???);
    	    
			startActivity(intent);
		}*/ else if(results.contains("There is no room")){
			//Toast.makeText(this, "No room with this name or qr or nfcid. :(", Toast.LENGTH_SHORT).show();
			noRoomFound("No room linked to this tag"); 
		} else {
			//Toast.makeText(this, "Something is wrong. " + results, Toast.LENGTH_SHORT).show();
			noRoomFound("Error: " + results);
		}


		
	}
	private void noRoomFound(String errorMessage){
		qrStatusText.setText(errorMessage);
		rescanButton.setVisibility(View.VISIBLE);
		manualLoginButton.setVisibility(View.VISIBLE);
		connectionProgress.setVisibility(View.INVISIBLE);
	}
	
	public void onClickRescan(View v){
		rescan();
	}
	private void rescan(){
		Log.d("lol", "mytag: rescan");
        qrStatusText.setText(R.string.qr_instruction);
        rescanButton.setVisibility(View.GONE);
        manualLoginButton.setVisibility(View.VISIBLE);
        if(isNFCPossibleOnDevice()){
			nfcInstructionText.setVisibility(View.VISIBLE);
		}
        startPreview();
        connectionProgress.setVisibility(View.INVISIBLE);
	}
	
	public void onClickManualEntry(View v){
		Intent intent = new Intent(this, JoinRoomActivity.class);
		startActivity(intent);
	}
	
	@Override
    protected void onResume() {
    	super.onResume();
    	if(!tagRead){
    		rescan();
    	}else{
    		tagRead = false;
    	}
    }

	@Override
	public FrameLayout getPreviewContainer() {
		return (FrameLayout)findViewById(R.id.qrCameraPreview);
	}
}
